# coding: utf-8
# flip bookmark

import sys
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QIcon, QIntValidator

from qfluentwidgets import (
    FluentIcon,
    CardWidget,
    ToolButton,
    LineEdit,
    BodyLabel,
    PushButton,
)


class PageTurningWidget(CardWidget):
    # 定义页码变化信号
    pageChangeSignal = pyqtSignal(int)

    """ 
    page : 总页码,默认为10
    currentPage : 当前页码
    numberButtonList : 页码按钮列表
    currentButtonNumbet : 当前显示的按钮数字列表
    """

    def __init__(self, parent=None, page=10):
        super().__init__(parent)
        self.page = page
        self.currentPage = 1
        self.numberButtonList = []
        self.currentButtonNumber = []

        # 定义翻页按钮
        self.leftPageButton = ToolButton(self)
        self.rightPageButton = ToolButton(self)

        # 定义跳转页面组件
        self.pageLineEdit = LineEdit(self)
        self.allPageLabel = BodyLabel(self)
        self.turnPageButton = PushButton(self)

        self.__initWidget()
        # 动态设置按钮
        self._addButton()

    def __initWidget(self):
        # 组件设置
        self.leftPageButton.setIcon(FluentIcon.PAGE_LEFT)
        self.leftPageButton.setFixedSize(40, 40)
        self.leftPageButton.clicked.connect(self.backPage)

        self.rightPageButton.setIcon(FluentIcon.PAGE_RIGHT)
        self.rightPageButton.setFixedSize(40, 40)
        self.rightPageButton.clicked.connect(self.forwardPage)

        self.pageLineEdit.setText("1")
        self.pageLineEdit.setValidator(QIntValidator())
        self.pageLineEdit.editingFinished.connect(self._validator)
        self.pageLineEdit.setFixedWidth(45)

        self.allPageLabel.setText(f"/{self.page}")

        self.turnPageButton.setText(self.tr("jump"))
        self.turnPageButton.setFixedWidth(60)
        self.turnPageButton.clicked.connect(self.turnPage)

        self.__initLayout()

    def __initLayout(self):
        # 布局设置
        self.layouts = QHBoxLayout(self)

        self.layouts.addWidget(self.leftPageButton)
        self.layouts.addWidget(self.rightPageButton)

        self.layouts.addSpacing(30)
        self.layouts.addWidget(self.pageLineEdit)
        self.layouts.addWidget(self.allPageLabel)
        self.layouts.addWidget(self.turnPageButton)

    # 向后翻页
    def forwardPage(self):
        if self.page > 5:
            if int(self.numberButtonList[-1].text()) < self.page:
                for i in self.numberButtonList:
                    i.setText(str(int(i.text()) + 1))

                for i in range(len(self.currentButtonNumber)):
                    self.currentButtonNumber[i] += 1
        if self.currentPage < self.page:
            self.currentPage += 1
            self.pageLineEdit.setText(str(self.currentPage))
            self.pageChangeSignal.emit(self.currentPage)

    # 向前翻页
    def backPage(self):
        if int(self.numberButtonList[0].text()) > 1:
            for i in self.numberButtonList:
                i.setText(str(int(i.text()) - 1))

            for i in range(len(self.currentButtonNumber)):
                self.currentButtonNumber[i] -= 1
        if self.currentPage > 1:
            self.currentPage -= 1
            self.pageLineEdit.setText(str(self.currentPage))
            self.pageChangeSignal.emit(self.currentPage)

    # 跳转页面
    def turnPage(self, page: int):
        """
        page : 目标跳转页
        """
        page = int(self.pageLineEdit.text())
        numberList = [1, 2, 3, 4, 5]
        if page not in numberList:

            while True:
                numberList = [x + 1 for x in numberList]
                if page in numberList and max(numberList) <= self.page:
                    break

        self.currentButtonNumber = numberList

        for i in self.numberButtonList:
            i.setText(str(self.currentButtonNumber[self.numberButtonList.index(i)]))
        self.pageChangeSignal.emit(page)
        self.currentPage = page

    # 输入框判断器,规定只可输入数字,并且数字不能超过规定范围
    def _validator(self):
        page = int(self.pageLineEdit.text())
        if page <= 0:
            page = 1
        elif page > self.page:
            page = self.page
        self.pageLineEdit.setText(str(page))

    # 动态添加按钮
    def _addButton(self):
        if self.page >= 5:
            for i in range(1, 6):
                numberButton = PushButton(str(i), self)
                numberButton.setFixedSize(40, 40)
                numberButton.clicked.connect(self._pageChanged)

                self.layouts.insertWidget(i, numberButton)
                self.numberButtonList.append(numberButton)
            self.currentButtonNumber = [1, 2, 3, 4, 5]

        else:
            for i in range(1, self.page + 1):
                numberButton = PushButton(str(i), self)
                numberButton.setFixedSize(40, 40)
                numberButton.clicked.connect(self._pageChanged)

                self.layouts.insertWidget(i, numberButton)
                self.numberButtonList.append(numberButton)
                self.currentButtonNumber.append(i)

    # 页面翻页时发出信号
    def _pageChanged(self, checked):
        sender = self.sender()
        if sender:
            button_text = sender.text()
            self.pageChangeSignal.emit(int(button_text))
            self.pageLineEdit.setText(button_text)
            self.currentPage = int(button_text)
